package com.neusoft.customerserver.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.neusoft.customerserver.dto.AddContacts;
import com.neusoft.customerserver.dto.RespBean;
import com.neusoft.customerserver.entity.Contacts;
import com.neusoft.customerserver.entity.Customer;
import com.neusoft.customerserver.entity.CustomerContacts;
import com.neusoft.customerserver.mapper.ContactsMapper;
import com.neusoft.customerserver.mapper.CustomerContactsMapper;
import com.neusoft.customerserver.mapper.CustomerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 客户联系人信息表 前端控制器
 * </p>
 *
 * @author victor
 * @since 2021-07-11
 */
@RestController
@RequestMapping("/customer")
public class ContactsController {
    @Autowired
    private ContactsMapper contactsMapper;
    @Autowired
    private CustomerContactsMapper cusConMapper;
    @Autowired
    private CustomerMapper customerMapper;

    @PostMapping("/addContacts")
    public RespBean addContacts(@RequestBody AddContacts addContacts){
        int num1 = 0;
        int num2 = 0;
        for (Contacts c : addContacts.getContactsList()){
            num1 += contactsMapper.insert(c);
        }
        for (int i = 0; i < addContacts.getContactsList().size(); i++){
            CustomerContacts cusCon = new CustomerContacts();
            cusCon.setCustomerId(addContacts.getCustomerId());
            cusCon.setContactsId(addContacts.getContactsList().get(i).getContactsId());
            num2 += cusConMapper.insert(cusCon);
        }

        if(num1 == addContacts.getContactsList().size() && num2 == addContacts.getContactsList().size()){
            return RespBean.ok(200,"Add Success",addContacts.getContactsList());
        }else {
            return RespBean.error(500,"ACCESS DENIED",addContacts.getContactsList());
        }
    }

    @PostMapping("/updateContacts")
    public RespBean updateContacts(@RequestBody Contacts contacts){
        int num = contactsMapper.updateById(contacts);
        if(num == 1){
            return RespBean.ok(200,"Update Success",contacts);
        }else {
            return RespBean.error(500,"ACCESS DENIED",contacts);
        }
    }

    @PostMapping("/deleteContactsById")
    public RespBean deleteContacts(@RequestBody Contacts contacts){
        QueryWrapper<CustomerContacts> qw2 = Wrappers.query();
        qw2.eq("contacts_id",contacts.getContactsId());
        int num1 = contactsMapper.deleteById(contacts.getContactsId());
        int num2 = cusConMapper.delete(qw2);
        if(num1 == 1 && num2 == 1){
            return RespBean.ok(200,"Delete Success",null);
        }else {
            return RespBean.error(500,"ACCESS DENIED");
        }
    }

    @PostMapping("/queryAllCustomerContacts")
    public RespBean queryAllCustomerContacts(@RequestBody Customer customer){
        QueryWrapper<Contacts> qw = Wrappers.query();
        QueryWrapper<CustomerContacts> qw2 = Wrappers.query();
        qw2.eq("customer_id",customer.getCustomerId());
        List<CustomerContacts> cusConList = cusConMapper.selectList(qw2);
        for (CustomerContacts cusCon : cusConList){
            qw.eq("contacts_id",cusCon.getContactsId());
        }
        List<Contacts> contactsList = contactsMapper.selectList(qw);
        if (contactsList.size() < 1){
            return RespBean.error(500,"No result",null);
        }else {
            return RespBean.ok(200,"Search success",contactsList);
        }

    }
}

